package Ticketing.test04;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author czl
 * @Date 2024-03-20
 */
public class Cinema implements Runnable{

    private int piao = 50;

    ReentrantLock reentrantLock =new ReentrantLock();

    @Override
    public void run() {
        while (piao >=1){
            sell();
        }
    }

    private synchronized void sell()  {

        try {
            reentrantLock.lock();

            if (piao>0){
                System.out.println(Thread.currentThread().getName()+"出售了第" + piao + "张票");
                piao--;
            }

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }finally {
            reentrantLock.unlock();
        }

    }

}
